<?php

declare(strict_types=1);

namespace App\Controller;

use App\Model\Article;
use App\Model\Category;
use Hyperf\HttpServer\Contract\RequestInterface;
use Hyperf\HttpServer\Contract\ResponseInterface;


/**
 * Class ArticleController
 * @package App\Controller
 */
class ArticleController extends AbstractController
{

    public function lists(RequestInterface $request, ResponseInterface $response)
    {


        $pageSize = (int)$request->input('pageSize', 10);
        $inputs = $request->inputs(['title', 'status', 'category_id']);
        $inputs['title'] && $where[] = ['title', 'like', '%' . $inputs['title'] . '%'];
        $inputs['status'] && $where[] = ['status', '=', $inputs['status']];
        $inputs['category_id'] && $where[] = ['category_id', '=', $inputs['category_id']];

        $records = Article::query()->where($where ?? [])->orderByDesc('id')
            ->paginate($pageSize, ['*'], 'currentPage');
        $items = $records->items();

        array_walk($items, function ($item) {
            $item->author_name = sprintf('ggc%s', rand(10, 100));
            $item->category_name = sprintf('分类%s', rand(10, 100));
        });
        return $response->json([
            'pagination' => [
                'total' => $records->total(),
                'pageSize' => $pageSize,
                'current' => $records->currentPage()

            ],
            'list' => $items
        ]);
    }

    public function batchDelete(RequestInterface $request, ResponseInterface $response)
    {
        $ids = $request->input('ids');
        if (!is_array($ids)) {
            return $response->json(['message' => '参数错误', 'status' => STATUS_FAILURE]);
        }
        Article::query()->whereIn('id', $ids)->delete();
        return $response->json(['message' => '操作成功', 'status' => STATUS_SUCCESS]);
    }

    public function toggleStatus(RequestInterface $request, ResponseInterface $response)
    {
        $status = $request->input('status');
        $ids = $request->input('ids');
        Article::query()->whereIn('id', $ids)->update([
            'status' => $status
        ]);
        return $response->json(['message' => '操作成功', 'status' => STATUS_SUCCESS]);
    }

    public function save(int $id, RequestInterface $request, ResponseInterface $response)
    {
        $inputs = $request->inputs(['title', 'category_id', 'labels', 'status', 'content', 'thumb']);
        $inputs['status'] = $inputs['status'] ? 1 : 0;
        Article::query()->updateOrCreate(['id' => $id], $inputs);
        return $response->json(['message' => '操作成功', 'status' => STATUS_SUCCESS]);
    }

    public function detail(int $id, ResponseInterface $response)
    {
        if ($id > 0) {
            $record = Article::query()->find($id);
        }

        $categoryList = Category::query()->get();
        return $response->json([
            'data' => [
                'article' => !empty($record) ? $record->toArray() : null,
                'categoryList' => $categoryList
            ],
            'status' => STATUS_SUCCESS
        ]);
    }
}
